package com.avic.rong.search.adapter.searchResultFragmentsAdapters;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.questionOrAnswer.AnswerViewHolder;
import com.avic.rong.questionOrAnswer.activity.AnswerActivity;
import com.avic.rong.questionOrAnswer.activity.PersonalHomeActivity;
import com.avic.rong.questionOrAnswer.activity.QuestionDetailsActivity;
import com.avic.rong.questionOrAnswer.api.AnswerThumbNumApi;
import com.avic.rong.questionOrAnswer.api.CancelThumbApi;
import com.avic.rong.questionOrAnswer.api.ThumbApi;
import com.avic.rong.questionOrAnswer.bean.QaaMyListBean;
import com.avic.rong.search.activity.SearchResaultActivity;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.GlideUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by XinEr on 2019/8/7.
 * Describe:
 */
public class SearchWenDaRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private int QUESTION = 100;
    private int ANSWER = 200;
    private Context context;
    private List<QaaMyListBean> listData;

    private boolean clickZan = true;
    private AnswerViewHolder viewHolder;

    private TextView tv_comment_num;
    private LinearLayout ll_comment;
    private int thumb = -1;
    private int clickPos = -1;
    private int contId;

    private int postion;
    public SearchWenDaRvAdapter(Context context, List<QaaMyListBean> listData) {
        this.listData = listData;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == QUESTION) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.question_item, viewGroup, false);
            return new QuestionViewHolder(inflate);
        } else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.answer_item, viewGroup, false);
            return new AnswerViewHolder(inflate);
        }


    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        //TODO  缺少字段
        QaaMyListBean qaaMyListBean = listData.get(i);
        if (viewHolder instanceof QuestionViewHolder) {
            ((QuestionViewHolder) viewHolder).ll_answaer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, AnswerActivity.class);
                    intent.putExtra(AnswerActivity.QUEID, qaaMyListBean.getId() + "");
                    context.startActivity(intent);
                }
            });
            ((QuestionViewHolder) viewHolder).tv_question.setText(qaaMyListBean.getTitle());
            if (!TextUtils.isEmpty(qaaMyListBean.getPublicTime1())) {
                ((QuestionViewHolder) viewHolder).tv_date.setText(qaaMyListBean.getPublicTime1());
            }
//            if (!TextUtils.isEmpty(SearchResaultActivity.getNAME())){
//                1
//                ((QuestionViewHolder) viewHolder).tv_name.setText(SearchResaultActivity.getNAME());
//            }
//
//            if (!TextUtils.isEmpty(SearchResaultActivity.getLOGO())){
//                GlideUtils.loaderHanldeRoundImage(context,SearchResaultActivity.getLOGO(),  ((QuestionViewHolder) viewHolder).ic_photo,80);
//            }
            ((QuestionViewHolder) viewHolder).ll_root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, QuestionDetailsActivity.class);
                    intent.putExtra("queID", qaaMyListBean.getId() + "");
                    context.startActivity(intent);
                }
            });

        } else if (viewHolder instanceof AnswerViewHolder) {
            ((AnswerViewHolder) viewHolder).setData(qaaMyListBean,context);
           /* ((AnswerViewHolder) viewHolder).tv_answer.setText(qaaMyListBean.getContent());
            ((AnswerViewHolder) viewHolder).tv_date.setText(qaaMyListBean.getPublicTime() + "");
            ((AnswerViewHolder) viewHolder).tv_question_content.setText(qaaMyListBean.getTitle() + "");
            ((AnswerViewHolder) viewHolder).tv_collect_num.setText(qaaMyListBean.getCollectNum() + "");
            ((AnswerViewHolder) viewHolder).tv_comment_num.setText(qaaMyListBean.getAppraiseNum() + "");
            ((AnswerViewHolder) viewHolder).tv_zan_num.setText(qaaMyListBean.getNiceNum() + "");
            //是否点赞
            if (qaaMyListBean.isThumb()) {
                ((AnswerViewHolder) viewHolder).iv_zan.setWdDrawerSelected(true);
            } else {
                ((AnswerViewHolder) viewHolder).iv_zan.setWdDrawerSelected(false);
            }


            //是否收藏
            if (qaaMyListBean.isFavorite()) {
                ((AnswerViewHolder) viewHolder).iv_collect.setWdDrawerSelected(true);
            } else {
                ((AnswerViewHolder) viewHolder).iv_collect.setWdDrawerSelected(false);
            }
            ((AnswerViewHolder) viewHolder).ll_zan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    contId = qaaMyListBean.getId();
                    if (((AnswerViewHolder) viewHolder).iv_zan.isSelected()) {
                        unZanReq(contId, context);
                    } else {
                        zanReq(contId, context);
                    }
                }
            });*/

        }
    }

    @Override
    public int getItemCount() {

        return listData.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (listData.get(position).getType() == 1) {
            return QUESTION;
        } else {
            return ANSWER;
        }

    }

    public class QuestionViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout ll_answaer;
        private ImageView ic_photo;
        private TextView tv_name;
        private TextView tv_date;
        private TextView tv_sign;
        private TextView tv_content;
        private TextView tv_question;
        private LinearLayout ll_root;

        public QuestionViewHolder(@NonNull View itemView) {
            super(itemView);
            ll_answaer = itemView.findViewById(R.id.ll_answaer);
            ic_photo = itemView.findViewById(R.id.ic_photo);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_date = itemView.findViewById(R.id.tv_date);
            tv_sign = itemView.findViewById(R.id.tv_sign);
            tv_content = itemView.findViewById(R.id.tv_content);
            tv_question = itemView.findViewById(R.id.tv_question);
            ll_root = itemView.findViewById(R.id.ll_root);
        }
    }

   /* public class AnswerViewHolder extends RecyclerView.ViewHolder {
        private ImageView ic_photo;
        private TextView tv_name;
        private TextView tv_date;
        private TextView tv_sign;
        private TextView tv_question_content;
        private FolderTextView tv_answer;
        private TextView tv_collect_num;
        private LinearLayout ll_collect;
        private TextView tv_zan_num;
        private LinearLayout ll_zan;
        private TextView tv_comment_num;
        private LinearLayout ll_comment;
        private ImageView iv_collect;
        private ImageView iv_zan;

        public AnswerViewHolder(@NonNull View itemView) {
            super(itemView);
            ic_photo = itemView.findViewById(R.id.ic_photo);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_date = itemView.findViewById(R.id.tv_date);
            tv_sign = itemView.findViewById(R.id.tv_sign);
            tv_question_content = itemView.findViewById(R.id.tv_question_content);
            tv_answer = itemView.findViewById(R.id.tv_answer);
            tv_collect_num = itemView.findViewById(R.id.tv_collect_num);
            ll_collect = itemView.findViewById(R.id.ll_collect);
            tv_zan_num = itemView.findViewById(R.id.tv_zan_num);
            ll_zan = itemView.findViewById(R.id.ll_zan);
            tv_comment_num = itemView.findViewById(R.id.tv_comment_num);
            ll_comment = itemView.findViewById(R.id.ll_comment);
            iv_collect = itemView.findViewById(R.id.iv_collect);
            iv_zan = itemView.findViewById(R.id.iv_zan);
        }
    }*/

    private void unZanReq(int contId, Context context) {
        PersonalHomeActivity personalHomeActivity = (PersonalHomeActivity) context;
        Map<String, String> map = new HashMap<>();

        if (contId <= 0 && TextUtils.isEmpty(SharedPreferencesUtil.getLoginToken())) {
            return;
        }

        map.put("contId", contId + "");

        //新闻1提问2回答3
        map.put("type", 3 + "");

        map.put("token", SharedPreferencesUtil.getLoginToken());
        final CancelThumbApi cancelThumbApi = new CancelThumbApi(personalHomeActivity, simpleOnNextListenerUnZan);
        cancelThumbApi.setCache(false);
        cancelThumbApi.setMap(map);
        HttpManager.getInstance().doHttpDeal(cancelThumbApi);


    }

    private void zanReq(int contId, Context context) {

        PersonalHomeActivity personalHomeActivity = (PersonalHomeActivity) context;
        Map<String, String> map = new HashMap<>();

        if (contId <= 0 && TextUtils.isEmpty(SharedPreferencesUtil.getLoginToken())) {
            return;
        }

        map.put("contId", contId + "");
        //
        //新闻1提问2回答3
        map.put("type", 3 + "");
        map.put("token",SharedPreferencesUtil.getLoginToken());
        final ThumbApi thumbApi = new ThumbApi(personalHomeActivity, simpleOnNextListener);
        thumbApi.setCache(false);
        thumbApi.setMap(map);
        HttpManager.getInstance().doHttpDeal(thumbApi);


    }

    //点赞
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)) {
                ToastTools.showToast(context, subjects.getMsg());
                return;
            }
            ToastTools.showToast(context, "点赞成功");
//            Double num = (Double) subjects.getData();
            String num = subjects.getData().toString();
            notifyDataSetChanged();
            thumbNum();

        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
        }
    };
    //取消点赞
    HttpOnNextListener simpleOnNextListenerUnZan = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)) {
                ToastTools.showToast(context, subjects.getMsg());
                return;
            }

            ToastTools.showToast(context, "取消点赞成功");
//            Double num = (Double) subjects.getData();
            String num = subjects.getData().toString();

            notifyDataSetChanged();
            thumbNum();
        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
        }
    };

    private void thumbNum() {
        PersonalHomeActivity personalHomeActivity = (PersonalHomeActivity) context;
        Map<String, String> map = new HashMap<>();

        if (contId <= 0) {
            return;
        }
        map.put("ansId", contId + "");

        final AnswerThumbNumApi answerThumbNumApi = new AnswerThumbNumApi(personalHomeActivity, simpleOnNextListenerThumbNum);
        answerThumbNumApi.setCache(false);
        answerThumbNumApi.setMap(map);
        HttpManager.getInstance().doHttpDeal(answerThumbNumApi);
    }

    //回答点赞数变动
    HttpOnNextListener simpleOnNextListenerThumbNum = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)) {
                ToastTools.showToast(context, subjects.getMsg());
                return;
            }

        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
        }
    };


}
